<?php

/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2020/10/16
 * Time: 9:39
 */
namespace app\common\service\asset_manage\host;

use app\common\service\BaseService;
use app\common\service\HostTag;
use think\Db;

class Host extends BaseService
{
    /**
     * @name 修改
     * @param $condition
     * @param $updateData
     * @return int|string
     * @author wx
     * @date 2019/10/24 13:55
     */
    public function modify($condition, $updateData)
    {
        return Db::name('host')->where($condition)->update($updateData);
    }
    /*
     * @name: 系统比率
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2019-06-04 14:00:04
     */
    public function osStatistics()
    {
        $osRelation = Db::name('Host')->group('os')->column('count(*)', 'os');
        $return = [];

        $hostModle = new \app\common\model\Host();
        foreach ($osRelation as $k => $v)
        {
            $child = [
                'name' => $hostModle->showOs($k),
                'value' => $v,
            ];
            $return[] = $child;
        }

        return $return;
    }

    /**
     * @name: 详情
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2019-10-25 11:24:18
     */
    public function info ($host_id)
    {
        $return = Db::name('Host')->alias('h')->find($host_id);

        return $return;
    }

    /**
     * @name: 添加
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2019-10-29 10:36:52
     */
    public function add($data)
    {
        $data['ctime'] = time();
        return Db::name('Host')->insertGetId($data);
    }

    /**
     * @name: 删除
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2019-10-29 13:21:29
     */
    public function uninstall ($host_id_a)
    {
        Db::name('Host')->whereIn('id', $host_id_a)->delete();
    }


    /**
     * @name: 查找通过id
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2019-11-15 16:18:50
     */
    public function findById ($host_id)
    {
        $return = Db::name('Host')->find($host_id);

        return $return;
    }

    /**
     * @name: 标签比率
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2019-11-20 17:07:20
     */
    public function tagStatistics ()
    {
        $HostTag = new HostTag();
        $tag_a = $HostTag->showTag([], 'name', 'id');
        $return = [];
        foreach ($tag_a as $k => $v)
        {
            $return[] = [
                'name' => $v,
                'value' => Db::name('Host')->where('FIND_IN_SET(' . "'" . $k . "'" . ',tag)')->count(),
            ];
        }

        return $return;
    }


    /**
     * @name: 存活主机
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2019-12-03 16:25:16
     */
    public function existNum()
    {
        $return = Db::name('Host')->count();

        return $return;
    }

    /**
     * @name 通过host_id获取ip
     * @param $host_id
     * @return mixed
     * @author wx
     * @date 2019/12/25 10:57
     */
    public function GetHostIp($host_id)
    {
        return Db::name('Host')->where('id',$host_id)->value('ip');
    }

    /**
     * @name 添加资产时生成hostkey
     * @author wx
     * @date 2020/12/1 17:26
     */
    public function createHostKey()
    {
        return md5(uniqid(md5(microtime(true)), true));
    }


}